#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	75342-nfs.sh
#Version:	1.1
#Date:		2013/11/20
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.04网络服务-05NFS-01字符模式-06使用/etc/fstab开机自动挂载NFS文件系统
#Notes:		NFS服务配置——/etc/fstab开机自动挂载
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/11/20
#		-The first one
#-----------------------------------------------------------------------------


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../../lib/XmlParse.sh
source ./lib/cmd.sh

function CleanData ()
{
  umount -f /mnt/rw
  
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "command -v systemctl && /bin/systemctl stop nfs.service || /sbin/service nfs stop"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "rm -rf /nfs-test"
  sed -i s/${ServerIP}.*$//g /etc/fstab
  rm -rf /mnt/rw
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo '' >/etc/exports"
  EchoInfo "75342-nfs.sh执行完毕"
}

trap "CleanData" EXIT INT
if [ -z "$SFROOT" ]
then
    echo "SFROOT is null"
    exit 1
fi

DATA_DIR="${SFROOT}/testcases/Network_Service"
DATA_FILE="config.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

GetValue ServerIP ReHost
GetValue USER UserName
GetValue PASSWORD RootPasswd

#DATA_DIR="./data"
#DATA_FILE="75342-nfs.xml"
#
#XmlParse ${DATA_DIR}/${DATA_FILE}
#
#GetValue USER USER
#GetValue ServerIP ServerIP
#GetValue PASSWORD PASSWORD

#远程配置服务器
EchoInfo "远程配置服务器${ServerIP}"

Scp "${USER}" "${ServerIP}" "${PASSWORD}" "./nfs-config.sh" "/root/"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sh nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "rm -rf /root/nfs-config.sh"

#客户端是否可以查看到nfs共享目录
EchoInfo "客户端能否看见nfs共享目录"
stop_daemon iptables  

test -n "`showmount -e ${ServerIP} |grep /nfs-test/readonly`" -a -n "`showmount -e ${ServerIP} |grep /nfs-test/rw`" 
EchoResult "客户端可以看见nfs共享目录"

#客户端自动挂载NFS文件系统
EchoInfo "客户端自动挂载NFS文件系统"
mount |grep /mnt/rw
[ $? -eq 0 ] && umount -f /mnt/rw

[ -d /mnt/rw ] || mkdir /mnt/rw
echo "${ServerIP}:/nfs-test/rw    /mnt/rw     nfs   defaults    0  0" >>/etc/fstab
mount -a
test -n "`mount |grep "${ServerIP}"`"
EchoResult "客户端自动挂载NFS文件系统成功" 

